package com.teamall.web.tssy.controller;

import java.sql.Timestamp;
import java.util.Date;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.teamall.core.common.BaseController;
import com.teamall.core.common.CommonEnum;
import com.teamall.core.common.JModelAndView;
import com.teamall.core.common.Page;
import com.teamall.framework.exception.BaseAppException;
import com.teamall.framework.log.LogManager;
import com.teamall.web.tssy.db.po.TsSyDeptPO;
import com.teamall.web.tssy.service.ITsSyDeptService;

/**
 * <Description>tssydept管理 <br>
 * 
 * @author codeCreater <br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.teamall.web.tssy.controller <br>
 */

@Controller
@RequestMapping("/tssy/tsSyDept")
public class TsSyDeptController extends BaseController {

    private static final LogManager logger = LogManager.getLogger(TsSyDeptController.class);

    @Autowired
    private ITsSyDeptService tsSyDeptService;
    
    @RequestMapping("index")
    public String index(Model model) {
        // ///////
        // TODO 根据业务场景，进行条件分支跳转、设置页面默认值等

        // ///////

        return "/page/tssy";
    }

    @RequestMapping("queryRecordByPage")
    public ModelAndView queryRecordByPage(TsSyDeptPO record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyDeptList",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	record.addNewConditions("isDel", CommonEnum.ISDEL_F.getCode());
    	Page<TsSyDeptPO> resultPage = tsSyDeptService.selectByArgAndPage(record, page);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("record", record);
    	
    	return mv;
    }

    @RequestMapping("queryRecordByPageForLookUp")
    public ModelAndView queryRecordByPageForLookUp(TsSyDeptPO record,
            Page page,HttpServletRequest request) throws BaseAppException {
    	logger.info("queryRecordByPage by record begin...record=[{0}]", record);
    	ModelAndView mv = new JModelAndView("tssy/tsSyDeptListLookup",request);
    	//将dwz分页参数转成Page对象
//    	Page page = dwzToPage(dwzPage);
    	record.addNewConditions("isDel", CommonEnum.ISDEL_F.getCode());
    	Page<TsSyDeptPO> resultPage = tsSyDeptService.selectByArgAndPage(record, page);
    	mv.addObject("resultPage", resultPage);
    	mv.addObject("record", record);
    	
    	return mv;
    }
    
    @RequestMapping("initAdd")
    public ModelAndView initAdd(TsSyDeptPO record,HttpServletRequest request) throws BaseAppException {
        logger.info("initAdd add begin...record=[{0}]", record);
        ModelAndView mv = new JModelAndView("tssy/tsSyDeptAdd", request);
        return mv;
    }
    
    @RequestMapping(value="add",method=RequestMethod.POST)
    public ModelAndView add(TsSyDeptPO record) throws BaseAppException {
        logger.info("add record begin...record=[{0}]", record);
        record.setIsDel(CommonEnum.ISDEL_F.getCode());
		record.setIsEnable(CommonEnum.ISENABLE_T.getCode());
		Timestamp tt=new Timestamp(new Date().getTime());
        record.setCreateTime(tt);
        record.setCreateBy(userId);
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyDeptService.add(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value="update",method=RequestMethod.PUT)
    public ModelAndView update(TsSyDeptPO record) throws BaseAppException {
        logger.info("update record begin...record=[{0}]", record);
        Timestamp tt=new Timestamp(new Date().getTime());
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyDeptService.update(record);
        return ajaxDoneSuccess(this.operSuccess);
    }

    @RequestMapping(value = "delete",method=RequestMethod.DELETE)
    public ModelAndView delete(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("delete record begin...id=[{0}]", id);
        tsSyDeptService.updateStatus(id);
        return ajaxDoneSuccess(operSuccess);
    }

    @RequestMapping("qryRecordInfo")
    public ModelAndView qryRecordInfo(@RequestParam(value = "id",
            required = true) String id,HttpServletRequest request) throws BaseAppException {
    	logger.info("qryRecordInfo by id begin...id=[{0}]", id);
    	ModelAndView mv = new JModelAndView("tssy/tsSyDeptAdd",request);
    	TsSyDeptPO record = tsSyDeptService.selectByPrimaryKey(id);
    	mv.addObject("record", record); 
    	return mv;  
    }

    /**
     * 启用部门
     * @param id
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value = "enable",method=RequestMethod.PUT)
    public ModelAndView enable(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("enable record begin...id=[{0}]", id);
        TsSyDeptPO record = tsSyDeptService.selectByPrimaryKey(id);
        record.setIsEnable(CommonEnum.ISENABLE_T.getCode());
        Timestamp tt=new Timestamp(new Date().getTime());
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyDeptService.update(record);
        return ajaxDoneSuccess(operSuccess);
    }
    
    /**
     * 禁用部门
     * @param id
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value = "disable",method=RequestMethod.PUT)
    public ModelAndView disable(@RequestParam(value = "id",required = true) String id) throws BaseAppException {
        logger.info("disable record begin...id=[{0}]", id);
        TsSyDeptPO record = tsSyDeptService.selectByPrimaryKey(id);
        record.setIsEnable(CommonEnum.ISENABLE_F.getCode());
        Timestamp tt=new Timestamp(new Date().getTime());
        record.setUpdateBy(userId);
        record.setUpdateTime(tt);
        tsSyDeptService.update(record);
        return ajaxDoneSuccess(operSuccess);
    }
    
    /**
     * 获取所有部门
     * @param record
     * @return
     * @throws BaseAppException
     */
    @RequestMapping(value="querryAll",method=RequestMethod.GET)
    @ResponseBody
    public List<TsSyDeptPO> querryAll(TsSyDeptPO record) throws BaseAppException {
    	List<TsSyDeptPO> result = tsSyDeptService.selectByArg(record);
    	return result;
    }
}
